<?php

namespace App\Http\Controllers\Backend\Auth;

use App\Entities\Auth\Manager;
use App\Http\Controllers\Backend\Controller;
use App\Services\Auth\ManagerService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use App\Entities\Auth\Role;

class ManagerController extends Controller
{
    protected $services;
    public function __construct(ManagerService $service)
    {
        $this->baseViewAddress = 'backend.auth.manager.';
        $this->service = $service;
    }

    public function list()
    {
        $data = $this->service->getPageList();

        return response()->json($data);
    }

    public function index()
    {
        return View::make($this->baseViewAddress.'index');
    }

    public function create()
    {
        $manager = new Manager();

        return View::make($this->baseViewAddress.'create', compact('manager'));
    }

    public function password($id, $type)
    {
        if ('auth' == $type) {
            return View::make('backend.changePassword', compact('id', 'type'));
        } else {
            return View::make($this->baseViewAddress.'changePassword', compact('id', 'type'));
        }
    }

    public function changePassword(Request $request, $id, $type)
    {
        $result = $this->service->changePassword($id, $request->all(), $type);
        if ($request->ajax() || $request->wantsJson()) {
            return response()->json([
                'status' => $result['flag'],
                'message' => $result['flag'] ? '修改成功' : $result['message'],
            ]);
        } else {
            if ($result['flag']) {
                return View::make($this->baseViewAddress.'changePassword', compact('id', 'type'));
            } else {
                return redirect(route('admin.auth.manager.index'))->with(['status' => $result['message']]);
            }
        }
    }

    public function role($id)
    {
        $roles = Role::all(['role_id', 'name']);

        return View::make($this->baseViewAddress.'role', compact('id', 'roles'));
    }

    public function saveRole($id, Request $request)
    {
        if ($this->service->saveRole($id, $request->get('role'))) {
            return redirect(route('admin.auth.manager.index'))->with(['status' => '分配成功']);
        }

        return redirect(route('admin.auth.manager.index'))->withErrors(['status' => '系统错误']);
    }

    public function store(Request $request)
    {
        if ($this->service->create($request->all())) {
            return redirect(route('admin.auth.manager.index'))->with(['status' => '添加成功']);
        }

        return redirect(route('admin.auth.manager.index'))->with(['status' => '系统错误']);
    }

    public function show($id)
    {
        $manager = $this->service->show($id);

        return View::make($this->baseViewAddress.'show', compact('manager'));
    }

    public function edit($id)
    {
        $manager = $this->service->show($id);
        if ('auth' == request()->get('type')) {
            return View::make('backend.edit');
        }

        return View::make($this->baseViewAddress.'edit', compact('manager'));
    }

    public function update(Request $request, $id)
    {
        $result = $this->service->update($id, $request->all());
        if ($request->ajax() || $request->wantsJson()) {
            return response()->json([
                'status' => $result,
                'message' => $result ? '更新成功' : '系统错误',
            ]);
        } else {
            if ($result) {
                return redirect(route('admin.auth.manager.index'))->with(['status' => '更新成功']);
            }

            return redirect(route('admin.auth.manager.index'))->withErrors(['status' => '系统错误']);
        }
    }

    public function destroy()
    {
        $ids = request()->get('ids');

        if (empty($ids)) {
            return response()->json(['code' => 1, 'msg' => '请选择删除项']);
        }
        if ($this->service->remove($ids)) {
            return response()->json(['code' => 0, 'msg' => '删除成功']);
        }

        return response()->json(['code' => 0, 'msg' => '删除成功']);
    }
}
